#include "DigitalPutOption.h"

double DigitalPutOption::payoff(double endStockPrice) const {
    if (endStockPrice >= getStrike()) {
        return 0.0;
    }
    else {
        return 1.0;
    }
}

double DigitalPutOption::payoff(const std::vector<double>& stockPrices) const {
    double stockAtMaturity = stockPrices.back();
    return payoff(stockAtMaturity);
}

double DigitalPutOption::price(
    const BlackScholesModel& bsm) const {
    MonteCarloPricer pricer;
    return pricer.price(*this, bsm);
}



//////////////////////////
//
//  Test the call option class
//
//
//////////////////////////

static void testDigitalPutOptionPayoff() {
    DigitalPutOption pc;
    pc.setStrike(105.0);
    pc.setMaturity(2.0);
    std::vector<double> d;
    d.push_back(110.0);
    ASSERT_APPROX_EQUAL(pc.payoff(d), 0.0, 0.001);
    d[0] = 100.0;
    ASSERT_APPROX_EQUAL(pc.payoff(d), 1.0, 0.001);
}

static void testDigitalPutOptionPrice() {
    DigitalPutOption pc;
    pc.setStrike(105.0);
    pc.setMaturity(2.0);

    BlackScholesModel bsm;
    bsm.date = 1.0;
    bsm.volatility = 0.001;
    bsm.riskFreeRate = 0.05;
    bsm.stockPrice = 100000.0;

    double price = pc.price(bsm);
    ASSERT_APPROX_EQUAL(price, 0.0, 0.01);
}

void testDigitalPutOption() {
    TEST(testDigitalPutOptionPrice);
    TEST(testDigitalPutOptionPayoff);
}